package com.grailslm.json

import groovy.json.JsonSlurper

class JsonRestore {
    def dsObject // a data structure of lists and maps

    JsonRestore(String strJson) {
        this.dsObject = new JsonSlurper().parseText(strJson)
    }

    /**
     * 还原修改
     * @param arrayList [[path:x.count, src:0], [path:x.data.list[0].srr.sflist[0].c_1_6_1_r_2, src:fffffff]]
     */
    void restoreModified(ArrayList<LinkedHashMap> arrayList) {
        arrayList.each {map->
            def path = map.get("path")
            def src = map.get("src")
            Eval.x(this.dsObject, "${path} = '${src}'")
        }
    }

    String getResultJson() {
        return this.dsObject
    }

    static main(args) {
        def restore = new JsonRestore(this.getResourceAsStream("target.json").text)

        restore.restoreModified([[path:"x.count", src:0], [path:"x.data.list[0].srr.sflist[0].c_1_6_1_r_2", src:"fffffff"]])

        println restore.getResultJson()
    }
}
